package org.come.Jpanel;

import com.tool.tcpimg.UIUtils;
import javafx.application.Platform;
import javafx.concurrent.Worker;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import org.come.bean.ConfigureBean;
import org.come.model.Configure;
import org.come.socket.DownLoadTxt;
import org.come.until.UserMessUntil;

import javax.swing.*;
import java.awt.*;
import java.math.BigDecimal;
import java.util.Map;

public class RechargeWebJpanel extends JPanel {
	private ImageIcon icon = new ImageIcon("inkImg/hongmu/ZHSBJ.png");
	private JFXPanel jfxPanel;
	private WebEngine engine;

	public RechargeWebJpanel() {
		// 设置面板大小
		this.setPreferredSize(new Dimension(1024 + 500, 1500));
		this.setLayout(null); // 使用绝对布局
		this.setBackground(UIUtils.Color_BACK);

		// 初始化 JFXPanel
		jfxPanel = new JFXPanel();
		jfxPanel.setBounds(150, 100, 1000, 600); // 设置 JFXPanel 的位置和大小
		this.add(jfxPanel);

		// 初始化 JavaFX 环境
		Platform.runLater(() -> initFX(jfxPanel));
	}

	// 初始化 JavaFX 部分
	private void initFX(JFXPanel fxPanel) {
		DownLoadTxt.getDownLoadTxt().initMes("configure.txt");
		ConfigureBean allConfigure = UserMessUntil.getConfigureBean();
		Map<BigDecimal, Configure> item = allConfigure.getAllConfigure();
		Configure configure = item.get(new BigDecimal(1));
		String url = configure.getJumpurl();
		// 创建 WebView 和 WebEngine
		WebView view = new WebView();
		engine = view.getEngine();
//		view.setZoom(1.2);
		// 创建场景并设置到 JFXPanel
		Scene scene = new Scene(view);
		fxPanel.setScene(scene);

		// 加载网页
		engine.load(url);

		// 设置网页缩放比例为 10 倍
		// 网页加载完成后执行缩放
		engine.getLoadWorker().stateProperty().addListener(
				(observable, oldValue, newValue) -> {
					if (newValue == Worker.State.SUCCEEDED) {
						// 缩小网页内容至90%
						engine.executeScript("document.body.style.zoom='80%';");
					}
				}
		);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		// 绘制背景图
		g.drawImage(icon.getImage(), 0, 0, 1024 + 500, 1500, this);
	}
}
